<html>
    <head>
        <title>NguyenThiPhuongHang</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>
    <body>
        <?php require_once 'model/db2.php'; ?>
        <h1>nguyen thi phuong hang</h1>
   <?php
                $companies = getPage("SELECT * FROM companies");
                var_dump($companies);                die();
                
                ?>
        <form method="POST">
            Min Like : <input type="text" name="Min"><br>
            Max Like:  <input type="text" name="Max"><br>
            Keyword : <input type="text" name="Keyword"><br>
            Company: <select name="id_company">
             
                <option value="0"><?php echo "<-----------Company----------->" ?></option>
                <?php foreach ($companies as $company): ?>
                    <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
                <?php endforeach; ?>
            </select> </br>
            Apps: <select name="id_app">
                <?php
                $apps = getApps("SELECT * FROM apps");
                ?>
                <option value="0"><?php echo "<------------------------Apps------------------------->" ?></option>
                <?php foreach ($apps as $app): ?>
                    <option value="<?php echo $app['id'] ?>"><?php echo $app['name'] ?></option>
                <?php endforeach; ?>
            </select></br>

            <input type="submit" name="submit" value="Search">
        </form>

        <?php
        $query = "SELECT * FROM pages";
        $pages = getPage($query);
        ?>
        <?php if (isset($_POST['submit'])): ?>
            <?php
            $min_fans = $_POST['Min'];
            $max_fans = $_POST['Max'];
            $keyword = $_POST['Keyword'];
            $company = $_POST["id_company"];
            $app = $_POST["id_app"];

            if (!empty($min_fans))
                $min_fans = "(likes >= $min_fans) AND";
            else
                $min_fans = "";
            if (!empty($max_fans))
                $max_fans = "(likes <= $max_fans) AND";
            else
                $max_fans = "";
            if (!empty($company))
                $company = "(companies.id= $company) AND";
            else
                $company = "";
            if (!empty($app))
                $app = "(apps.id= $app) AND";
            else
                $app = "";


            $query = "SELECT * FROM pages
                    INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                    INNER JOIN apps ON apps.id=apps_pages.id_app
                    INNER JOIN companies ON companies.id=apps.id_company
                  WHERE $min_fans
                        $max_fans
                        $company
                        $app
                        (description LIKE '% $keyword %')
                        LIMIT 0, 10 
                ";
            var_dump($query);
            $pages = getPage($query);
            ?> 
            <?php if (!empty($pages)): ?>

                <table border ="1">
                    <tr>
                        <td class="pid">STT </td>
                        <td class="picture">picture </td>
                        <td class="id">ID</td>
                        <td class="name">Name</td>
                        <td class="likes">Likes</td>
                        <td class="talking">Talking</td>
                        <td class="category">Category</td>
                        <td class="link">Link </td>
                        <td class="website">Website</td>
                        <td class="country">Country </td>
                    </tr>
                    <?php foreach ($pages as $page): ?>
                        <tr>
                            <td><?php echo $page["pid"] ?></td>
                            <td><img src = '<?php echo $page["picture"] ?>'></td>
                            <td><?php echo $page["id"] ?></td>
                            <td><?php echo $page["name"] ?></td>
                            <td><?php echo $page["likes"] ?></td>
                            <td><?php echo $page["talking_about_count"] ?></td>
                            <td><?php echo $page["category"] ?></td>
                            <td><?php echo $page["link"] ?></td>
                            <td><?php echo $page["website"] ?></td>
                            <td><?php echo $page["country"] ?></td>
                        </tr>
                    <?php endforeach; ?>
                </table>
            <?php endif; ?>
        <?php endif; ?>

        <script type =" text/javascrip">
        alert ("heo kich vao di")
        </script>
        <script>
            $("#min_fans").click(function(){
                alert($("#min_fans").val());
                
            });
                    </script>
                    <script>
                    $(document).ready(function(){
                        $('#check').click(function(){
                            var min_fans = $('#min_fans').val();
                            var flag = checkNumber (min_fans);
                            if(fag){
                            
                                alert('is number');
                                
                            }else{
                                alert('is number');       
                            }
                        });
                        function checkNumber(val){
                            if($.isNumeric(val)){
                                return true;
                            }else{
                                return fale;
                            }
                        }
                    });
                    </script>
    </body>
</html>